<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-input v-model="chapterName" clearable placeholder="输入章节名称搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-input v-model="courseName" clearable placeholder="输入课程名称搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-input v-model="userName" clearable placeholder="输入学员姓名搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-select v-model="status" clearable placeholder="审核状态" class="filter-item">
        <el-option v-for="item in statusInfo" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
      <el-button
        :disabled="data.length === 0 || $refs.table.selection.length === 0"
        class="filter-item"
        size="mini"
        type="warning"
        icon="el-icon-circle-check"
        @click="open">审核
      </el-button>
    </div>
    <el-dialog
      title="审核内容"
      :append-to-body="true"
      :visible.sync="centerDialogVisible"
      width="35%">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
       <el-form-item label="审核状态" prop="status" class="dd-label">
          <!-- <el-radio v-model="form.status" :label="1">审核通过</el-radio>
          <el-radio v-model="form.status" :label="2">审核不通过</el-radio> -->
          <el-select v-model="form.status" style="width: 440px" placeholder="审核状态">
            <el-option v-for="item in isHotList" :key="item.value" :label="item.label" :value="item.value-0">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核说明" prop="remark" class="dd-label">
          <el-input type="textarea" :rows="4" placeholder="请输入内容" style="width: 440px" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="centerDialogVisible=false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
    <el-table v-loading="loading" :data="data" ref="table" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column prop="userName" label="学员姓名" width="100" align="center"/>
      <el-table-column prop="chapterName" label="章节名称" width="200" align="center"/>
      <el-table-column prop="courseName" label="课程名称" width="200" align="center"/>
      <el-table-column prop="answerContent" label="问答内容" :show-overflow-tooltip='true' align="center"/>
      <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>
      <el-table-column label="审核状态" align="center" width="120">
        <template slot-scope="scope">
          <div v-for="item in statusInfo" :key="item.value">
            <el-tag v-if="scope.row.status === item.value" :type="item.type">{{ item.label }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="审核说明" :show-overflow-tooltip='true' align="center" width="300"/>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
              </el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
  import { del, check } from '@/api/business/answer'
  import initData from '@/mixins/initData'

  export default {
    mixins: [initData],
    data() {
      return {
        loading: false,
        delLoading: false,
        form: {
          ids: [],
          status: null,
          remark: ''
        },
        centerDialogVisible: false,
        isHotList:[{value:1,label:"审核通过"},{value:2,label:"审核不通过"}],
        statusInfo: [
          { value: 0, label: '未审核', type: 'info' },
          { value: 1, label: '审核通过', type: 'success' },
          { value: 2, label: '审核不通过', type: 'danger' }
        ],
        status: null,
        userName: '',
        chapterName: '',
        courseName: '',
        rules: {
          status: [
            { required: true, message: '请输入课程名称', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '请输选择课程分类', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      beforeInit() {
        this.url = 'web/answer/page-list'
        this.params = {
          page: this.page,
          size: this.size,
          courseName: this.courseName,
          chapterName: this.chapterName,
          userName: this.userName,
          status: this.status
        }
        return true
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
          this.init()
        })
        this.delLoading = false
        this.$refs[id].doClose()
      },
      open() {
        const data = this.$refs.table.selection
        const ids = []
        data.forEach(item => ids.push(item.id))
        this.form.ids = ids
        this.centerDialogVisible = true
      },
      doSubmit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            check(this.form).then(res => {
              this.$notify({
                title: '操作成功',
                type: 'success',
                duration: 2500
              })
              this.loading = false
              this.init()
              this.centerDialogVisible = false
            }).catch(err => {
              this.loading = false
              console.log(err)
            })
          }
        })
      }
    }
  }
</script>
